package cn.mujiankeji.mbrowser.界面.框架

import android.annotation.SuppressLint
import androidx.activity.compose.BackHandler
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import cn.mujiankeji.mbrowser.app.界面模块
import cn.mujiankeji.mbrowser.工具.mlog
import cn.mujiankeji.mbrowser.数据.配置项.应用配置
import cn.mujiankeji.mbrowser.界面.testview
import com.blankj.utilcode.util.ScreenUtils
import org.koin.compose.koinInject


/**
 * 应用的顶层界面 Composable，负责整体布局。
 */
@SuppressLint("UnusedBoxWithConstraintsScope")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun 界面实现() {
    // 使用 Koin 获取 ViewModel 实例
    val viewModel: 界面模块 = koinInject()
    BoxWithConstraints {
        viewModel.屏高 = maxHeight
        viewModel.屏宽 = maxWidth
        Column(Modifier.fillMaxSize()) {
            界面主视图()
        }
        界面小窗视图()
        testview(viewModel)

        全屏内容()

        // 显示全局加载对话框
        加载中对话框()
    }
    mlog("ddd")
    BackHandler {
        viewModel.后退()
    }
}



@Composable
fun 全屏内容(viewModel: 界面模块 = koinInject()){
    if (viewModel.全屏视图!=null){
        Box(Modifier.fillMaxSize().background(Color.Black)){
            viewModel.全屏视图?.invoke()
        }
    }

}


/**
 * 全局加载中对话框
 * 
 * @param 提示文本 加载中的提示文本
 */
@Composable
fun 加载中对话框(viewModel: 界面模块 = koinInject()) {
    if (viewModel.加载中提示文本.isNotEmpty()){
        Dialog(onDismissRequest = {}) {
            Surface(
                shape = RoundedCornerShape(8.dp),
                color = MaterialTheme.colorScheme.surface,
                shadowElevation = 8.dp
            ) {
                Column(
                    modifier = Modifier.padding(16.dp),
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    CircularProgressIndicator(
                        modifier = Modifier.size(40.dp),
                        color = MaterialTheme.colorScheme.primary
                    )

                    Spacer(modifier = Modifier.height(16.dp))

                    Text(
                        text = viewModel.加载中提示文本.ifEmpty { "加载中..." },
                        style = MaterialTheme.typography.bodyLarge,
                        textAlign = TextAlign.Center
                    )
                }
            }
        }
    }
}



